package com.moonshineproject.view.panel
{
	import com.moonshineproject.view.IPanelWindow;
	import com.moonshineproject.view.divider.IDEVDividedBox;
	
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.utils.Timer;
	
	import mx.core.UIComponent;
	import mx.events.EffectEvent;
	import mx.events.FlexEvent;
	
	import spark.effects.Rotate3D;
	
	public class PanelRotationEffect
	{
		protected var sidebar:IDEVDividedBox;
		protected var oldPanel:IPanelWindow;
		protected var newPanel:IPanelWindow;
		
		protected var bitmap:Bitmap;
		protected var container:UIComponent;
		
		public function PanelRotationEffect(sidebar:IDEVDividedBox, oldPanel:IPanelWindow, newPanel:IPanelWindow)
		{
			this.sidebar = sidebar;
			this.oldPanel = oldPanel;
			this.newPanel = newPanel;
		}

		public function play():void
		{
			// make panels the same size
			newPanel.width = oldPanel.width;
			newPanel.height = oldPanel.height;
			
			// Add new panel to stage so it can draw
			newPanel.includeInLayout = false;
			newPanel.visible = false;
			var idx:int = sidebar.getChildIndex(oldPanel as DisplayObject);
			sidebar.addChildAt(newPanel as DisplayObject, idx);
			
			// Let it do stuff, then animate
			// TODO: Figure out when it's ready to spin for maximal goodlooks
			sidebar.callLater(animate);
		}
		
		// TODO: Make this a nice cube rotation effect
		protected function animate(evt:Object=null):void
		{
			var effect:Rotate3D = new Rotate3D(oldPanel);
			effect.autoCenterProjection = true;
			effect.autoCenterTransform = true;
			effect.angleYTo = 90;
			effect.duration = 300;
			effect.addEventListener(EffectEvent.EFFECT_END, rotationDone);
			effect.play();	
		}
		
		protected function rotationDone(event:Event):void
		{
			sidebar.removeChild(oldPanel as DisplayObject);
			
			newPanel.includeInLayout = true;
			newPanel.visible = true;
			
			var effect:Rotate3D = new Rotate3D(newPanel);
			effect.autoCenterProjection = true;
			effect.autoCenterTransform = true;
			effect.angleYFrom = -90;
			effect.angleYTo = 0;
			effect.duration = 300;
			effect.play();
		}

	}
}